from django.shortcuts import render,HttpResponse
from .models import Question
# Create your views here.

def index(request):
    return HttpResponse('哇哈哈哈')

def add_question(request):
    #展示收集界面
    if request.method=='GET':
        return render(request,'add_question.html')

   #接收前台传递过来的数据  保存到数据库
    if request.method=='POST':
        title=request.POST.get('title')
        qtype = request.POST.get('qtype')
        option1 = request.POST.get('option1')
        option2 = request.POST.get('option2')
        option3= request.POST.get('option3')
        option4 = request.POST.get('option4')
        ref_answer = request.POST.get('re_answer')
        analysis = request.POST.get('analysis')

        q1=Question(
            title=title,
            qtype=qtype,
            option1=option1,
            option2=option2,
            option3=option3,
            option4=option4,
            ref_answer=ref_answer,
            analysis=analysis
        )
        q1.save()
        result='插入成功'
        return render(request,'add_question.html',{'result':result})


def query_question(request):
    if request.method=='GET':
        questions=[]
        return render(request,'query_question.html',{'questions':questions})

    if request.method=='POST':
        wd=request.POST.get('wd')
        print(wd)

        questions=Question.objects.filter(title__contains=wd)
        print(type(questions))
        print(questions)
        return render(request,'query_question.html',{'questions':questions})


